* This do file runs the the results presented in Nigus, Nillesen and Mohnen 2023
* It makes use of one data file:
*   - Nigus et al 2023.dta 
* The do file does the following:
* All tables and stats in the paper
* All tables and stats in the appendix



capture log close
set more off
clear 


// set directory
if "`c(username)'" == "halefomnigus" {
	global projectfolder ".../Replication files/"
}

cd "$projectfolder"




*** Load data
use "JRI-Feb-20-019.dta", clear 



*** Generate new variables 
gen age_square=age*age
label variable age_square "Age square of the household head"

gen tlu = own_ox*1+own_bull*1+own_cow*1+ own_heifer*0.75+ own_calves*0.25 ///
              +own_horse*1+own_sheep*0.13+own_goat*0.13+ own_donkey*0.70  ///
			  + own_camel*1.25+ own_chicken*0.013    
label variable tlu "Tropical livestock unit for the household"

gen educ=0
replace educ=1 if education!=0
label define educ 1 "Literate" 0 "Illiterate", replace
label variable educ "Educational status of the household head"


gen housing_condition=0
replace housing_condition=1 if house_condition>=2
label define housing_condition 1 "Average and above" 0 "Leaking or falling apart", replace
label variable housing_condition "House condition"

 
gen roofing=0
replace roofing=1 if roof>2
label define roofing 1 "corrugated metal" 0 "Thatch or Plastic", replace
label variable roofing "Roof of the house"




*** Sampling Weights

gen strata=.
replace strata=1 if village_code==1 & buy_wii==1
replace strata=2 if village_code==1 & buy_wii==0
replace strata=3 if village_code==2 & buy_wii==1
replace strata=4 if village_code==2 & buy_wii==0
replace strata=5 if village_code==5 & buy_wii==1
replace strata=6 if village_code==5 & buy_wii==0
replace strata=7 if village_code==7 & buy_wii==1
replace strata=8 if village_code==7 & buy_wii==0
replace strata=9 if village_code==8 & buy_wii==1
replace strata=10 if village_code==8 & buy_wii==0


label define Strata 1 "Strata 1" 2 "Strata 2" 3 "Strata 3" 4 "Strata 4" 5 "Strata 5" ///
                    6 "Strata 6" 7 "Strata 7" 8 "Strata 8" 9 "Strata 9" 10 "Strata 10", replace
label values strata Strata



gen Population=.
replace Population=398 if village_code==1 & buy_wii==1
replace Population=1877 if village_code==1 & buy_wii==0
replace Population=625 if village_code==2 & buy_wii==1
replace Population=1327 if village_code==2 & buy_wii==0
replace Population=368 if village_code==5 & buy_wii==1
replace Population=1506 if village_code==5 & buy_wii==0
replace Population=425 if village_code==7 & buy_wii==1
replace Population=1620 if village_code==7 & buy_wii==0
replace Population=357 if village_code==8 & buy_wii==1
replace Population=1461 if village_code==8 & buy_wii==0

gen weight=.
replace weight=Population/24
replace weight=weight/100




global hhcxs "age sex active_people educ land_owned tlu roofing watch_tv listen_radio live religioo risk_preference"
global controls "$hhcxs"



********************************************************************************
******************************** Main Anlaysis  ******************************** 
********************************************************************************

************************
* Descriptive Analysis *
************************

***************
*** Figures ***
***************

*** Figure 1
//Contribution to the Public Good Game, 95% CI
cibar tokens_shared, over1(buy_wii) barlabel(on) bargap(50) barcolor(navy*.45 navy*.75) 

*** Figure 2
//CDF Plot Distribution of Contributions to the Public Good
cdfplot tokens_shared if buy_wii!=., by(buy_wii) norm

*** Figure 3
// Private Transfers & Financial Contribution Across Insured & Uninsured Groups, 95% CI
cibar give_transfer, over1(buy_wii) barlabel(on) bargap(50) barcolor(navy*.45 navy*.75) 
cibar contributions_comm_proj, over1(buy_wii) barlabel(on) bargap(50) barcolor(navy*.45 navy*.75) 



**************
*** Tables ***
**************

*** Table 2 
// Two-sample Kolmogorov-Smirnov test  - WII uptake
ksmirnov tokens_shared, by(buy_wii)
ksmirnov give_transfer, by(buy_wii)
ksmirnov contributions_comm_proj, by(buy_wii)


*** Table 3
// WII purchase and social capital - Matching results 
xi: psmatch2 buy_wii $controls, /// 
kernel k(epan) bwidth(0.06) out(tokens_shared) // kernel matching with the default kernel type and bandwidth
outreg2 using Table3.xls, alpha (0.01, 0.05, 0.1) dec(3) replace 


*** Table 4
// Matching Quality Test (WII uptake)  
xi: psmatch2 buy_wii  $controls, ///
kernel k(epan) bwidth(0.06) out(tokens_shared) // kernel matching with the default kernel type and bandwidth
pstest, both



*** Table 5
// Tests on the Validity of the Selected Instrument 

preserve 
xi:probit buy_wii dist_foreman [pweight=weight], robust
mfx2, replace
outreg2 using Table5.xls, alpha (0.01, 0.05, 0.1) dec(3) replace 
xi:probit buy_wii dist_foreman $controls [pweight=weight], robust
mfx2, replace
outreg2 using Table5.xls, alpha (0.01, 0.05, 0.1) dec(3) append 

xi:regress tokens_shared dist_foreman [pweight=weight] if buy_wii==0, robust
outreg2 using Table5.xls, alpha (0.01, 0.05, 0.1) dec(3) append  
xi:regress tokens_shared dist_foreman $controls [pweight=weight] if buy_wii==0, robust
outreg2 using Table5.xls, alpha (0.01, 0.05, 0.1) dec(3) append 

xi:regress tokens_shared dist_foreman [pweight=weight] if buy_wii==1, robust
outreg2 using Table5.xls, alpha (0.01, 0.05, 0.1) dec(3) append  
xi:regress tokens_shared dist_foreman $controls [pweight=weight] if buy_wii==1, robust
outreg2 using Table5.xls, alpha (0.01, 0.05, 0.1) dec(3) append  

xi:regress tokens_shared dist_foreman $controls [pweight=weight], robust
outreg2 using Table5.xls, alpha (0.01, 0.05, 0.1) dec(3) append  
xi:regress tokens_shared buy_wii dist_foreman $controls [pweight=weight], robust
outreg2 using Table5.xls, alpha (0.01, 0.05, 0.1) dec(3) append  
restore 




*** Table 6
// IV Estimates on the Effect of WII on Social Capital (Experimental Measures)

preserve 
xi:ivreg2 tokens_shared i.village_code (buy_wii=dist_foreman) [pweight=weight] , robust  first 
outreg2 using Table6.xls, alpha (0.01, 0.05, 0.1) dec(3) replace adds(KP F-Stat, e(rkf))

xi:ivreg2 tokens_shared $controls (buy_wii=dist_foreman) [pweight=weight] , robust  first 
outreg2 using Table6.xls, alpha (0.01, 0.05, 0.1) dec(3) append adds(KP F-Stat, e(rkf))

xi:ivreg2 tokens_shared $controls i.village_code (buy_wii=dist_foreman) [pweight=weight], robust first 
outreg2 using Table6.xls, alpha (0.01, 0.05, 0.1) dec(3) append adds(KP F-Stat, e(rkf))
restore



*** Table 7
// The Effect of WII on Social Capital (Survey Measures)

preserve 
xi:probit give_transfer buy_wii $controls i.village_code [pweight=weight] , robust
mfx2, replace
outreg2 using Table7.xls, alpha (0.01, 0.05, 0.1) dec(3) replace

xi:ivreg2 give_transfer (buy_wii= dist_foreman) $controls i.village_code [pweight=weight], robust first
outreg2 using Table7.xls, alpha (0.01, 0.05, 0.1) dec(3) append adds(KP F-Stat, e(rkf)) 

xi:probit contributions_comm_proj buy_wii $controls i.village_code [pweight=weight], robust
mfx2, replace
outreg2 using Table7.xls, alpha (0.01, 0.05, 0.1) dec(3) append

xi:ivreg2 contributions_comm_proj (buy_wii= dist_foreman) $controls i.village_code [pweight=weight], robust first
outreg2 using Table7.xls, alpha (0.01, 0.05, 0.1) dec(3) append adds(KP F-Stat, e(rkf))  
restore




************************************
* Balance Test by Access to WII    *
************************************

/* The data is available upon formal request from the 
Ethiopian Statistical Service (former Central Statistical Agency (CSA)).

While we are unable to share the data in this manuscript, 
the following code can be used to replicate the balance test
*/


*** Table 8
// Balance Test by Access to WII Using the 2007 Census Data

cap use "$projectfolder/POPULATION REC 10 percent.dta", clear 

keep if C01_REGION==1                                              
keep if C02_ZONE==4                                                  
keep if C03_WEREDA>=6 & C03_WEREDA<=7                             
keep if P04_RELATION==1                                          

replace P05_SEX=0 if P05_SEX==2
label define P05_SEX 1 "Male" 0 "Female", replace
gen sex=P05_SEX
gen age= P06_AGE

gen religion=0
replace religion=1 if P07_RELIGION==1
replace religion=. if P07_RELIGION==2
replace religion=. if P07_RELIGION==3
replace religion=. if P07_RELIGION==5
replace religion=. if P07_RELIGION==6 

gen ethnicity=0
replace ethnicity=1 if P09_ETHNIC==80

gen language=0
replace language=1 if P08_MTONGUE==80

gen literacy=0
replace literacy=1 if P18_LITERACY==1

gen attending_school=0
replace attending_school=1 if literacy ==1

gen employment= P24_EMPSTATUS
replace employment=0 if employment==.
replace employment=1 if employment>0

gen WII_access=.                                                 // Generate access to WII 
replace WII_access=1 if C07_KEBELE==2 & C03_WEREDA==6
replace WII_access=1 if C07_KEBELE==3 & C03_WEREDA==6
replace WII_access=1 if C07_KEBELE==9 & C03_WEREDA==6
replace WII_access=0 if C07_KEBELE==4 & C03_WEREDA==6
replace WII_access=0 if C07_KEBELE==6 & C03_WEREDA==6

replace WII_access=1 if C07_KEBELE==1 & C03_WEREDA==7
replace WII_access=1 if C07_KEBELE==3 & C03_WEREDA==7
replace WII_access=0 if C07_KEBELE==2 & C03_WEREDA==7

gen age_square=age*age
gen marital_status= P25_MARSTATUS

estpost ttest age age_square sex marital_status literacy attending_school employment religion, by(WII_access)
esttab using  wii.xls, replace wide cells (" mu_1(f(4)) mu_2(f(4)) b(f(4)) p") star ( 0.1  0.05 ** 0.01) width(1.1\hsize) label
eststo clear




*** Table 9
//The Impact of Access to WII on Social Capital

preserve
xi: probit buy_wii $controls
predict yhat 
tab yhat
sum yhat, detail
drop if buy_wii==0

xi: psmatch2 wii_tabia  $controls , ///
kernel k(epan) bwidth(0.06) out(tokens_shared) 

xi: reg tokens_shared wii_tabia  if _support==1  , robust 
outreg2 using Table9.xls, alpha (0.01, 0.05, 0.1) dec(3) replace

xi: reg tokens_shared wii_tabia $controls  if _support==1 , robust 
outreg2 using Table9.xls, alpha (0.01, 0.05, 0.1) dec(3) append

xi: reg tokens_shared wii_tabia $controls gussed_contbn if _support==1, robust 
outreg2 using Table9.xls, alpha (0.01, 0.05, 0.1) dec(3) append
restore


preserve
xi: probit buy_wii $controls
predict yhat 
tab yhat
sum yhat, detail
drop if buy_wii==1

xi: psmatch2 wii_tabia  $controls , ///
kernel k(epan) bwidth(0.06) out(tokens_shared) 

xi: reg tokens_shared wii_tabia  if _support==1  , robust 
outreg2 using Table9.xls, alpha (0.01, 0.05, 0.1) dec(3) append

xi: reg tokens_shared wii_tabia $controls  if _support==1 , robust 
outreg2 using Table9.xls, alpha (0.01, 0.05, 0.1) dec(3) append

xi: reg tokens_shared wii_tabia $controls gussed_contbn if _support==1, robust 
outreg2 using Table9.xls, alpha (0.01, 0.05, 0.1) dec(3) append

restore





*** Table 10
// IV Estimates on the Socio-Psychological Effects of Weather Index Insurance

xi:ivreg2 village_fair (buy_wii=dist_foreman) [pweight=weight],  robust first 
xi:ivreg2 success (buy_wii= dist_foreman) [pweight=weight],  robust first 

xi:ivreg2 village_fair (buy_wii=dist_foreman) [pweight=weight],  robust first
outreg2 using Table10.xls, alpha (0.01, 0.05, 0.1) dec(3) replace adds(KP F-Stat, e(rkf)) 

xi:ivreg2 village_fair (buy_wii=dist_foreman) $controls  [pweight=weight],  robust first
outreg2 using Table10.xls, alpha (0.01, 0.05, 0.1) dec(3) append adds(KP F-Stat, e(rkf)) 

xi:ivreg2 village_fair (buy_wii=dist_foreman) $controls  i.village_code [pweight=weight],  robust first
outreg2 using Table10.xls, alpha (0.01, 0.05, 0.1) dec(3) append adds(KP F-Stat, e(rkf)) 

xi:ivreg2 success (buy_wii= dist_foreman) [pweight=weight],  robust first
outreg2 using Table10.xls, alpha (0.01, 0.05, 0.1) dec(3) append adds(KP F-Stat, e(rkf)) 

xi:ivreg2 success (buy_wii= dist_foreman) $controls [pweight=weight],  robust first
outreg2 using Table10.xls, alpha (0.01, 0.05, 0.1) dec(3) append adds(KP F-Stat, e(rkf)) 

xi:ivreg2 success (buy_wii= dist_foreman) $controls i.village_code [pweight=weight],  robust first
outreg2 using Table10.xls, alpha (0.01, 0.05, 0.1) dec(3) append adds(KP F-Stat, e(rkf)) 





********************************************************************************
********************************    Appendix.   ******************************** 
********************************************************************************

**************
*** Tables ***
**************

// Full-sample: Summary statistics 
tabulate education, gen (edu) 



***Table A1
// Summary Statistics

sutex age sex active_people edu1 edu2 edu3 edu4 tlu land_owned roofing /// 
watch_tv listen_radio religioo risk_preference live gussed_contbn ///
dist_foreman, label nobs key(descstat) replace ///
file(TableA1.xls) title("Summary statistics") minmax



***Table A2
// Descriptive Statistics by WII Uptake
estpost ttest tokens_shared  age sex active_people edu1 edu2 edu3 edu4 /// 
tlu land_owned roofing watch_tv listen_radio religioo risk_preference live /// 
dist_foreman, by(buy_wii)
esttab using  TableA2.xls, replace wide cells (" mu_1(f(4)) mu_2(f(4)) b(f(4)) p") star ( 0.1  0.05 ** 0.01) width(1.1\hsize) label
eststo clear



***Table A3
// Sub-sample: Descriptive statistics by access to insurance 
estpost ttest   age sex active_people edu1 edu2 edu3 edu4 tlu land_owned roofing /// 
watch_tv listen_radio religioo risk_preference live, by(wii_tabia)
esttab using  TableA3.xls, replace wide cells (" mu_1(f(4)) mu_2(f(4)) b(f(4)) p") star ( 0.1  0.05 ** 0.01) width(1.1\hsize) label
eststo clear



*** Table A4
// Covariate balance test - Matching quality test
xi: psmatch2 buy_wii age sex active_people educ tlu land_owned /// 
roofing  watch_tv listen_radio live religioo risk_preference, ///
kernel k(epan) bwidth(0.06) out(tokens_shared)   // kernel matching with the default kernel type and bandwidth. 
pstest, both

cap outreg2 using TableA4.xls, alpha (0.01, 0.05, 0.1) dec(3) replace 



**** Table A5
// Determinants of distance to the foreman's residence
xi:reg dist_foreman age sex active_people educ land_owned tlu roofing religioo ///
risk_preference i.village_code, robust

outreg2 using TableA5.xls, alpha (0.01, 0.05, 0.1) dec(3) replace  




*** Table A6
// The Effect of WII on Technology Adoption

preserve 
xi:ivreg2 fertilizer (buy_wii= dist_foreman) $controls [pweight=weight], robust first
outreg2 using TableA6.xls, alpha (0.01, 0.05, 0.1) dec(3) replace adds(KP F-Stat, e(rkf))

xi:ivreg2 fertilizer (buy_wii= dist_foreman) $controls i.village_code [pweight=weight], robust first
outreg2 using TableA6.xls, alpha (0.01, 0.05, 0.1) dec(3) append adds(KP F-Stat, e(rkf))

xi:ivreg2 improved_seed (buy_wii= dist_foreman) $controls [pweight=weight], robust first
outreg2 using TableA6.xls, alpha (0.01, 0.05, 0.1) dec(3) append adds(KP F-Stat, e(rkf)) 

xi:ivreg2 improved_seed (buy_wii= dist_foreman) $controls i.village_code [pweight=weight], robust first
outreg2 using TableA6.xls, alpha (0.01, 0.05, 0.1) dec(3) append adds(KP F-Stat, e(rkf)) 
restore




***************
*** Figures ***
***************

*** Figure 4
// Propensity score distribution and common support for propensity score estimation 

xi: psmatch2 buy_wii  $controls i.village_code, ///
kernel k(epan) bwidth(0.06) out(tokens_shared)
psgraph
graph export "psgraph.eps", replace 



*** Figure 5
// Kernel density distribution of propensity scores for treated and control groups

teffects psmatch (tokens_shared) (buy_wii $controls  i.village_code), atet
teffects overlap, kernel(epanechnikov)
graph export "teffects.eps", replace  



*** Figure 6
// Contribution to the Public Good Game, 95% CI
cibar tokens_shared, over1(wii_tabia) barlabel(on) bargap(50) barcolor(navy*.45 navy*.75) 



*** Figure 7
//CDF Plot Distribution of Contributions to the Public Good
cdfplot tokens_shared, by(wii_tabia) norm









